<?php


namespace App\Exports;

use App\Models\RecordOrder;
use Maatwebsite\Excel\Concerns\FromView;
use Illuminate\Contracts\View\View;

/**
 * Created By FreeAbrams
 * Date: 2020/11/30
 */
class RoomExport implements FromView
{
	protected $timeBetween;
	
	public function __construct($timeBetween)
	{
		$this->timeBetween = $timeBetween;
	}
	
	public function view():View
	{
		$temp = $this->data($this->timeBetween);
		
		$sum = 0;
		foreach ($temp as $v){
			$sum += $v->count();
		}

		return view('admin.export.room-export-table', ['data' => $temp, 'sum' => $sum]);
	}
	
	protected function data($timeBetween)
	{
		$recodeOrders = RecordOrder::whereBetween('created_at', $timeBetween)
			->with(['order.room' => function($q){
				$q->with(['scale' => function($q){
					$q->withTrashed()->get();
				}])->withTrashed();
			}])
			->get('order_id');
		// 按照房间名称分组
		return $recodeOrders->groupBy('order.room.scale.name');
	}
}